def do(n):
  if n == 1 or n == 0:
      return False
  a = 0
  for i in range(2, int(n ** 0.5) + 1):
      if (n % i == 0):
          a = a + 1
  if (a == 0):
      return True
  else:
      return False




def generate(a):
  a = str(a)
  results = set()
  for i in range(len(a)):
      for j in range(i, len(a)):
          results.add(a[i:j+1])
  return(results)




x = int(input())
if x < 1000 or x > 9999:
  print(0)
else:
   sp = generate(x)
   sp = [int(x) for x in sp]
   sp.sort(reverse=True)
   for x in sp:
       if do(x):
           print(x)
           break
   else:
       print(0)
